package cn.e4j.modular.api.controller;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.modular.api.service.Gwat200Service;
import cn.e4j.modular.api.service.Gwat600Service;
import cn.e4j.modular.api.util.CommonUtils;
import cn.e4j.modular.api.util.ControllerSupport;
import cn.e4j.modular.system.entity.SysUser;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 安装信息回报 控制类
 * Created by yuan_rz on 2018/5/10.
 */
@RestController
@RequestMapping("api/wx/gwat600")
public class Gwat600Controller extends ControllerSupport {

    @Autowired
    Gwat600Service gwat600Service;

    @Autowired
    Gwat200Service gwat200Service;

    /**
     * 获取订单信息
     *
     * @return
     */
    @RequestMapping(value = "getGwcvDatas")
    public ResponseData getGwcsDatas(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        List<Dto> list = new ArrayList<Dto>();
        SysUser currentuser = wapper.getSysUser();
        try {
            // 获取区域权限-进行省市过滤数据
            inDto.put("GWAA003", currentuser.getACCOUNT());
            inDto.put("MMRE004S",gwat200Service.getMmreData(inDto));

            list = gwat600Service.getGwcvData(wapper.getPage(),inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS(list);
    }

    /**
     * 回报 -- 单笔
     *
     * @return
     */
    @RequestMapping("confirm")
    public ResponseData confirm(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        try {
            inDto.put("type", "single");
            JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(inDto));
            String result = gwat600Service.validate(jsonObject);
            if (ToolUtil.isEmpty(result)) {
                gwat600Service.confirm(wapper.getSysUser(),jsonObject);
            } else {
                return ERROR(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS;
    }

    /**
     * 回报 -- 批量
     *
     * @return
     */
    @RequestMapping("confirmlist")
    public ResponseData confirmlist(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        String errmsg = "ok";
        List<Dto> customerList = new ArrayList<Dto>();
        try {
            JSONArray items = JSON.parseArray(inDto.getString("items"));
//            List<Dto> items = AppJson.fromJson();
            for (Object obj : items) {
                JSONObject item = JSONObject.parseObject(JSON.toJSONString(obj));
                item.put("type", "multiple");
                String result = gwat600Service.validate(item);
                if (ToolUtil.isEmpty(result)) {
                    Dto customer = gwat600Service.confirm(wapper.getSysUser(),item);
                    customerList.add(customer);
                } else {
                    if ("ok".equals(errmsg)) {
                        errmsg = result;
                    } else {
                        errmsg = errmsg + result;
                    }
                    return ERROR(errmsg);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS(customerList);
    }

    /**
     * 取消回报 -- 单笔
     *
     * @return
     */
    @RequestMapping("reconfirm")
    public ResponseData reconfirm(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        try {
            Dto item = JSONObject.parseObject(inDto.getString("item"), Dto.class);
            JSONArray arr = (JSONArray)item.get("list");
            String gwcsdocno = ToolUtil.isEmpty(arr)?"":((JSONObject)arr.get(0)).getString("GWCSDOCNO");
            inDto.put("GWCSDOCNO",gwcsdocno);
            String result = gwat600Service.check(inDto);
            if (ToolUtil.isEmpty(result)) {
                gwat600Service.reconfirm(item);
            } else {
                return ERROR(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS;
    }

    /**
     * 取消回报 -- 批量
     *
     * @return
     */
    @RequestMapping("reconfirmlist")
    public ResponseData reconfirmlist(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        String errmsg = "ok";
        List<Dto> docnoList = new ArrayList<Dto>();
        try {
            List<Dto> items = CommonUtils.toJavaList(inDto.getString("items"),Dto.class);
            for (Dto item : items) {
                String result = gwat600Service.check(item);
                if (ToolUtil.isEmpty(result)) {
                    Dto docno = gwat600Service.reconfirm(item);
                    docnoList.add(docno);
                } else {
                    if ("ok".equals(errmsg)) {
                        errmsg = result;
                    } else {
                        errmsg = errmsg + result;
                    }
                    return ERROR(errmsg);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS(docnoList);
    }

    /**
     * 扫码增加商品
     *
     * @return
     */
    @RequestMapping("addGood")
    public ResponseData addGood(@DtoParam DtoWapper wapper) {
        Dto inDto = wapper.getDto();
        Dto good = new Dto();
        SysUser currentUSer = wapper.getSysUser();
        try {
            // 获取区域权限-进行省市过滤数据
            inDto.put("GWAA003", currentUSer.getACCOUNT());
            inDto.put("MMRE004S",gwat200Service.getMmreData(inDto));

            good = gwat600Service.addGood(inDto);
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
        return SUCCESS(good);
    }

}
